package ai.llz.ai_pro;

import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.dictionary.CustomDictionary;
import com.hankcs.hanlp.seg.Segment;
import com.hankcs.hanlp.seg.common.Term;
import com.hankcs.hanlp.tokenizer.StandardTokenizer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

import java.util.List;
import java.util.Scanner;

@Service
public class TableService {

    @Resource
    private TableMapper mapper;

    public String getResult(String input) throws Exception {
        StringPair pair = getPairByHanLPSegament(input);

        if (pair == null){
            return "对不起，小元无法识别您的输入，请重新输入哦";
        }


        if (pair.getKey().isEmpty()){
            pair.setKey("描述");
        }

        String name = pair.getName();
        String key = pair.getKey();


        List<Entity> value = mapper.getValueEle(name, key);
        if (value.isEmpty()){
            value = mapper.getValue(name,key);
        }
        if (value.isEmpty()){
            pair.setName(name+key);
            pair.setKey("描述");
            value = mapper.getValue(name+key,"描述");
        }
        if (value.isEmpty()){
            pair.setName(name);
            pair.setKey("描述");
            value = mapper.getValue(name,"描述");
        }
        if (value.isEmpty()){
            return "对不起，这个问题小元暂时还不会哦，您可以更换关键词试试";
        }

        if (value.size() == 1){
            return value.get(0).getValue();
        }


        StringBuilder builder = new StringBuilder();

        if (pair.getKey().equals("描述")){
            for (Entity entity : value) {
                builder.append(entity.getValue());
                builder.append("\n");
            }
            builder.deleteCharAt(builder.length()-1);
        }
        else{
            builder.append(name);
            builder.append('的');
            for (Entity entity : value) {
                builder.append(entity.getKey());
                builder.append("是");
                builder.append(entity.getValue());
                builder.append(",");
            }
            builder.deleteCharAt(builder.length()-1);
            builder.append('。');
        }
        return builder.toString();
    }

    private StringPair getPairBySimpleSplit(String value){
        int i = value.indexOf('的');
        return new StringPair(value.substring(0,i),value.substring(i+1));
    }



    @Resource
    Segment segment;

    private StringPair getPairByHanLPSegament(String input) throws IOException {

        List<Term> terms = segment.seg(input);
        List<SegamentResult> results = new ArrayList<>();
        int i = 0;
        for (Term term : terms) {
            results.add(new SegamentResult(term,i));
            i++;
        }
        results.sort(SegamentResult::compareTo);
        System.out.println(results);
        if (results.isEmpty()){
            return null;
        }
        if (results.size() == 1){
            return new StringPair(results.get(0).getWord(),"描述");
        }
        return new StringPair(results.get(0).getWord(),results.get(1).getWord());
    }

}
